package com.bh.web.controller.ledger;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.ledger.domain.JcLedgerDetail;
import com.bh.ledger.service.IJcLedgerDetailService;
import com.bh.ledger.vo.JcCustomerLedgerDetail;
import com.bh.ledger.vo.JcLedgerDetailGenerateVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 费用账单Controller
 *
 * @author slx
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/ledger/detail")
public class JcLedgerDetailController extends BaseController {
    @Autowired
    private IJcLedgerDetailService jcLedgerDetailService;

    /**
     * 查询费用账单列表
     */
    @ApiOperation("查询费用账单列表")
    @PreAuthorize("@ss.hasPermi('ledger:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcLedgerDetail jcLedgerDetail) {
        startPage();
        List<JcLedgerDetail> list = jcLedgerDetailService.selectJcLedgerDetailList(jcLedgerDetail);
        return getDataTable(list);
    }


    /**
     * 手动生成账单
     *
     */
    @ApiOperation("手动生成账单")
    @PreAuthorize("@ss.hasPermi('ledger:detail:list')")
    @PostMapping("/manualGenerate")
    public AjaxResult manualGenerate(@RequestBody @Validated JcLedgerDetailGenerateVo generationVo) {
        return toAjax(jcLedgerDetailService.manualGenerate(generationVo));
    }



    /**
     * 查询费用账单列表
     */
    @ApiOperation("查询费用账单列表")
    @PreAuthorize("@ss.hasPermi('ledger:detail:list')")
    @GetMapping("/customerList")
    public TableDataInfo customerList(JcCustomerLedgerDetail jcLedgerDetail) {
        startPage();
        List<JcCustomerLedgerDetail> list = jcLedgerDetailService.selectJcCustomerLedgerDetailList(jcLedgerDetail);
        return getDataTable(list);
    }


    @ApiOperation("查询核销页面台账费用明细列表")
    @GetMapping(value = "/getOrderFeeList/{orderId}")
    public AjaxResult getOrderFeeList(@PathVariable Long orderId) {
        List<JcLedgerDetail> list = jcLedgerDetailService.selectJcLedgerDetailListByOrderId(orderId);
        return AjaxResult.success(list);
    }


    @ApiOperation("查询干线成本页面台账费用明细列表")
    @GetMapping(value = "/getShipmentFeeList/{shipmentId}")
    public AjaxResult getShipmentFeeList(@PathVariable Long shipmentId) {
        List<JcLedgerDetail> list = jcLedgerDetailService.selectJcLedgerDetailListByShipmentId(shipmentId);
        return AjaxResult.success(list);
    }

    /**
     * 导出费用账单列表
     */
    @PreAuthorize("@ss.hasPermi('ledger:detail:export')")
    @Log(title = "费用账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcLedgerDetail jcLedgerDetail) {
        List<JcLedgerDetail> list = jcLedgerDetailService.selectJcLedgerDetailList(jcLedgerDetail);
        ExcelUtil<JcLedgerDetail> util = new ExcelUtil<>(JcLedgerDetail.class);
        util.exportExcel(response, list, "费用账单数据");
    }

    /**
     * 获取费用账单详细信息
     */
    @ApiOperation("获取费用账单详细信息")
    @PreAuthorize("@ss.hasPermi('ledger:detail:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcLedgerDetailService.selectJcLedgerDetailById(id));
    }

    /**
     * 新增费用账单
     */
    @ApiOperation("新增费用账单")
    @PreAuthorize("@ss.hasPermi('ledger:detail:add')")
    @Log(title = "费用账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcLedgerDetail jcLedgerDetail) {
        return toAjax(jcLedgerDetailService.insertJcLedgerDetail(jcLedgerDetail));
    }

    /**
     * 修改费用账单
     */
    @ApiOperation("修改费用账单")
    @PreAuthorize("@ss.hasPermi('ledger:detail:edit')")
    @Log(title = "费用账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcLedgerDetail jcLedgerDetail) {
        if (jcLedgerDetail.getIsConfirm() == 1) {
            return AjaxResult.error("已确认的台账不能修改！");
        }
        return toAjax(jcLedgerDetailService.updateJcLedgerDetail(jcLedgerDetail));
    }

    /**
     * 删除费用账单
     */
    @ApiOperation("删除费用账单")
    @PreAuthorize("@ss.hasPermi('ledger:detail:remove')")
    @Log(title = "费用账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<JcLedgerDetail> jcLedgerDetails = jcLedgerDetailService.listByIds(Arrays.asList(ids));
        for (JcLedgerDetail jcLedgerDetail : jcLedgerDetails) {
            if (jcLedgerDetail.getIsConfirm() == 1) {
                return AjaxResult.error("已确认的台账不能删除！");
            }
        }
        return toAjax(jcLedgerDetailService.deleteJcLedgerDetailByIds(ids));
    }


    /**
     * 确认费用单据
     */
    @ApiOperation("确认费用单据")
    @PostMapping( "/confirm/{id}")
    public AjaxResult confirm(@PathVariable Long id, Date date) {
        JcLedgerDetail jcLedgerDetail = jcLedgerDetailService.getById(id);
        if (jcLedgerDetail.getIsConfirm() == 1) {
            return AjaxResult.error("已确认的台账不能再次确认！");
        }
        return toAjax(jcLedgerDetailService.confirm(id, date));

    }

    /**
     * 反确认费用单据
     */
    @ApiOperation("反确认费用单据")
    @PostMapping( "/unconfirmed/{id}")
    public AjaxResult unconfirmed(@PathVariable Long id) {
        JcLedgerDetail jcLedgerDetail = jcLedgerDetailService.getById(id);
        if (jcLedgerDetail.getIsConfirm() == 0) {
            return AjaxResult.error("未确认的台账不能反确认！");
        }
        return toAjax(jcLedgerDetailService.unconfirmed(id));
    }
}
